<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info"
                  show-icon/>

        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('user.user.User name') + '/' + t('user.user.nickname') })"
        />

        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table ref="tableRef"/>

        <!-- 表单 -->
        <PopupForm/>
        <Log :dialogVisible="dialogVisible" :row="Data" @close="logClose"></Log>
    </div>
</template>

<script setup lang="ts">
import {ref, provide} from 'vue'
import baTableClass from '/@/utils/baTable'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import {defaultOptButtons} from '/@/components/table'
import {baTableApi} from '/@/api/common'
import {useI18n} from 'vue-i18n'
import Log from "./Log.vue";

defineOptions({
    name: 'user/user',
})


// defaultOptButtons 函数返回公共按钮数据（OptButton[]）
let optBtn = defaultOptButtons(['edit', 'delete'])
// 给第一个按钮加上点击事件

const dialogVisible = ref(false)
const Data = ref()
const logClose = () => {
    dialogVisible.value = false
}
// 自定义一个新的按钮
let newButton: OptButton[] = [
    {
        // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
        render: 'tipButton',
        // 按钮名称
        name: 'info',
        // 鼠标放置时的 title 提示
        title: '余额日志',
        // 直接在按钮内显示的文字，title 有值时可为空
        text: '余额日志',
        // 按钮类型，请参考 element plus 的按钮类型
        type: 'primary',
        // 按钮 icon
        icon: 'fa fa-search-plus',
        class: 'table-row-info',
        // tipButton 禁用 tip
        disabledTip: false,
        // 自定义点击事件
        click: (row: TableRow, field: TableColumn) => {
            dialogVisible.value = true
            console.log("dialogVisible.value = true",dialogVisible.value)

            Data.value = row
        },
        // 按钮是否显示，请返回布尔值
        display: (row: TableRow, field: TableColumn) => {
            return true
        },
        // 按钮是否禁用，请返回布尔值
        disabled: (row: TableRow, field: TableColumn) => {
            return false
        },
        // 自定义el-button属性
        attr: {}
    },
]

// 新按钮合入到默认的按钮数组
optBtn = newButton.concat(optBtn)


const {t} = useI18n()
const tableRef = ref()
const baTable = new baTableClass(
    new baTableApi('/admin/user.User/'),
    {
        column: [
            {type: 'selection', align: 'center', operator: false},
            {label: t('Id'), prop: 'id', align: 'center', operator: '=', operatorPlaceholder: t('Id'), width: 70},
            {label: t('user.user.head portrait'), prop: 'avatar', align: 'center', render: 'image', operator: false},

            {
                label: t('user.user.User name'),
                prop: 'username',
                align: 'center',
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query')
            },
            {
                label: t('user.user.nickname'),
                prop: 'nickname',
                align: 'center',
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query')
            },
            // {
            //     label: t('user.user.grouping'),
            //     prop: 'group.name',
            //     align: 'center',
            //     operator: 'LIKE',
            //     operatorPlaceholder: t('Fuzzy query'),
            //     render: 'tag',
            // },
            // {
            //     label: t('user.user.Gender'),
            //     prop: 'gender',
            //     align: 'center',
            //     render: 'tag',
            //     custom: {'0': 'info', '1': '', '2': 'success'},
            //     replaceValue: {'0': t('Unknown'), '1': t('user.user.male'), '2': t('user.user.female')},
            // },
            {
                label: '余额',
                prop: 'money',
                align: 'center',
            },
            {
                label: t('user.user.mobile'),
                prop: 'mobile',
                align: 'center',
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query')
            },
            // {
            //     label: t('user.user.Last login IP'),
            //     prop: 'last_login_ip',
            //     align: 'center',
            //     operator: 'LIKE',
            //     operatorPlaceholder: t('Fuzzy query'),
            //     render: 'tag',
            // },
            {
                label: t('user.user.Last login'),
                prop: 'last_login_time',
                align: 'center',
                render: 'datetime',
                sortable: 'custom',
                operator: 'RANGE',
                width: 160,
            },
            {
                label: t('Create time'),
                prop: 'create_time',
                align: 'center',
                render: 'datetime',
                sortable: 'custom',
                operator: 'RANGE',
                width: 160
            },
            {
                label: t('State'),
                prop: 'status',
                align: 'center',
                render: 'tag',
                custom: {disable: 'danger', enable: 'success'},
                replaceValue: {disable: t('Disable'), enable: t('Enable')},
            },
            {
                label: t('Operate'),
                align: 'center',
                render: 'buttons',
                buttons: optBtn,
                operator: false,
                width: 200
            },
        ],
        showComSearch:true,
        dblClickNotEditColumn: [undefined],
    },
    {
        defaultItems: {
            gender: 0,
            money: '0',
            score: '0',
            status: 'enable',
        },
    }
)

baTable.mount()
baTable.getIndex()

provide('baTable', baTable)
</script>

<style scoped lang="scss"></style>
